﻿using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core.Entity;

namespace NewRetail.Application.Components.Platform
{
    public static class PlatformExtension
    {
        public static void ConfigurePlatformEntities(this ModelBuilder modelBuilder)
        {
            var seqBuilder = modelBuilder.Entity<SequenceEntity>().ConfigBaseEntity();
            seqBuilder.ToTable("tb_sequence");
            seqBuilder.Property(x => x.Category).IsRequired().HasMaxLength(100);
            seqBuilder.Property(x => x.NextId).IsConcurrencyToken();
            seqBuilder.HasIndex(x => x.Category).IsUnique();

            //付款结算单
            var paymentReceiptBuilder = modelBuilder.Entity<PaymentReceipt>().ConfigBaseEntity().ConfigReceiptEntity();
            paymentReceiptBuilder.ToTable("payment_receipt");
            paymentReceiptBuilder.Property(x => x.supplierID).IsRequired().HasMaxLength(50);
            paymentReceiptBuilder.Property(x => x.money).IsRequired().HasPrecision(18, 4);
            paymentReceiptBuilder.Property(x => x.remark).HasMaxLength(1000);
            paymentReceiptBuilder.Ignore(x => x.Supplier);

            //收银折扣
            var postDiscountBuilder = modelBuilder.Entity<PosDiscount>().ConfigBaseEntity();
            postDiscountBuilder.ToTable("pos_Discount");
            postDiscountBuilder.Property(x => x.flowNo).IsRequired().HasMaxLength(100);
            postDiscountBuilder.Property(x => x.cotMoney).HasPrecision(18, 4);
            postDiscountBuilder.Property(x => x.cashierId).HasMaxLength(100);
            postDiscountBuilder.Property(x => x.cashierName).HasMaxLength(100);

            //收银员
            var posCashierBuilder = modelBuilder.Entity<PosCashier>()
                .ConfigBaseEntity()
                .ConfigNo()
                .ConfigWarehouse();

            posCashierBuilder.ToTable("pos_cashier");
            posCashierBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            posCashierBuilder.Property(x => x.cashierName).IsRequired().HasMaxLength(50);
            posCashierBuilder.Property(x => x.password).IsRequired().HasMaxLength(50);
            posCashierBuilder.Property(x => x.authorizationCode).HasMaxLength(100);
            posCashierBuilder.Ignore(x => x.Warehouse);

            //收银机
            var posCashRegisterBuilder = modelBuilder.Entity<PosCashRegister>()
                .ConfigBaseEntity()
                .ConfigNo()
                .ConfigWarehouse();

            posCashRegisterBuilder.ToTable("pos_cash_register");
            posCashRegisterBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            posCashRegisterBuilder.Property(x => x.cashRegisterName).IsRequired().HasMaxLength(50);
            posCashRegisterBuilder.Ignore(x => x.Warehouse);

            //会员积分表
            var integralBuilder = modelBuilder.Entity<PosIntegral>().ConfigBaseEntity();
            integralBuilder.ToTable("pos_integral");
            integralBuilder.Property(x => x.memberID).IsRequired().HasMaxLength(50);
            integralBuilder.Property(x => x.orderNo).HasMaxLength(100);
            integralBuilder.Property(x => x.integralSource).IsRequired().HasMaxLength(100);
            integralBuilder.Property(x => x.plusOrMinus);
            integralBuilder.Property(x => x.integral).IsRequired().HasPrecision(18, 4);
            integralBuilder.Ignore(x => x.PosMember);

            //积分规则表
            var integralRuleBuilder = modelBuilder.Entity<PosIntegralRule>().ConfigBaseEntity().ConfigReceiptEntity().ConfigWarehouse();
            integralRuleBuilder.ToTable("pos_integral_rule");
            integralRuleBuilder.Property(x => x.memberTypeID).IsRequired().HasMaxLength(50);
            integralRuleBuilder.Property(x => x.integralRule).IsRequired().HasPrecision(18, 4);
            integralRuleBuilder.Property(x => x.remark).HasMaxLength(200);
            integralRuleBuilder.Ignore(x => x.Warehouse);

            //会员表
            var posMemberBuilder = modelBuilder.Entity<PosMember>().ConfigBaseEntity().ConfigNo();
            posMemberBuilder.ToTable("pos_member");
            posMemberBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            posMemberBuilder.Property(x => x.memberTypeID).IsRequired().HasMaxLength(50);
            posMemberBuilder.Property(x => x.sourceWarehouseID).IsRequired().HasMaxLength(50);
            posMemberBuilder.Property(x => x.startDate).HasColumnType("datetime");
            posMemberBuilder.Property(x => x.endDate).HasColumnType("datetime");
            posMemberBuilder.Property(x => x.memberName).HasMaxLength(50);
            posMemberBuilder.Property(x => x.sex);
            posMemberBuilder.Property(x => x.memberTel).HasMaxLength(50);
            posMemberBuilder.Property(x => x.memberBirthday).HasMaxLength(20);
            posMemberBuilder.Property(x => x.cardAmount).HasPrecision(18, 4);
            posMemberBuilder.Property(x => x.TotalConsAmount).HasPrecision(18, 4);
            posMemberBuilder.Property(x => x.integral).HasPrecision(18, 4);
            posMemberBuilder.Property(x => x.cashierID).HasMaxLength(50);
            posMemberBuilder.Ignore(x => x.PosCashier).Ignore(x => x.Warehouse).Ignore(x => x.PosMemberType);

            //会员类型
            var posMemberTypeBuilder = modelBuilder.Entity<PosMemberType>().ConfigBaseEntity().ConfigNo();
            posMemberTypeBuilder.ToTable("pos_member_type");
            posMemberTypeBuilder.Property(x => x.memberTypeName).HasMaxLength(50);
            posMemberTypeBuilder.Property(x => x.discount).HasPrecision(18, 4);
            posMemberTypeBuilder.Property(x => x.isIntegral);
            posMemberTypeBuilder.Property(x => x.isStoredValue);
            posMemberTypeBuilder.Property(x => x.integralDiscount).HasPrecision(18, 4);

            //日结
            var posPayDaySumBuilder = modelBuilder.Entity<PosDaySum>()
                .ConfigBaseEntity()
                .ConfigWarehouse();

            posPayDaySumBuilder.ToTable("pos_day_sum");
            posPayDaySumBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            posPayDaySumBuilder.Property(x => x.saleNum).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.returnNum).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.giveNum).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.saleAmount).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.returnAmount).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.giveAmount).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.savePrice).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.preNum).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.preAmount).HasPrecision(18, 4);
            posPayDaySumBuilder.Property(x => x.remark).HasMaxLength(1000);
            posPayDaySumBuilder.Property(x => x.opreateTime).HasColumnType("datetime");
            posPayDaySumBuilder.Ignore(x => x.Warehouse).Ignore(x => x.GoodsInfo);

            //支付记录
            var posPayFlowBuilder = modelBuilder.Entity<PosPayFlow>()
                .ConfigBaseEntity()
                .ConfigWarehouse();

            posPayFlowBuilder.ToTable("pos_pay_flow");
            posPayFlowBuilder.Property(x => x.flowNo).IsRequired().HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.saleAmount).IsRequired().HasPrecision(18, 4);
            posPayFlowBuilder.Property(x => x.payAmount).IsRequired().HasPrecision(18, 4);
            posPayFlowBuilder.Property(x => x.payWay).IsRequired().HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.cardNumber).HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.memberID).HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.cashierID).HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.cashierName).HasMaxLength(50);
            posPayFlowBuilder.Property(x => x.payTime).HasColumnType("datetime");
            posPayFlowBuilder.Ignore(x => x.PosCashier);


            //销售流水
            var posSalesFlowBuilder = modelBuilder.Entity<PosSalesFlow>()
                .ConfigBaseEntity()
                .ConfigWarehouse();

            posSalesFlowBuilder.ToTable("pos_sales_flow");
            posSalesFlowBuilder.Property(x => x.flowNo).IsRequired().HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.categoryID).IsRequired().HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.sourcePrice).IsRequired().HasPrecision(18, 4);
            posSalesFlowBuilder.Property(x => x.salePrice).IsRequired().HasPrecision(18, 4);
            posSalesFlowBuilder.Property(x => x.saleQuantity).IsRequired().HasPrecision(18, 4);
            posSalesFlowBuilder.Property(x => x.saleMoney).IsRequired().HasPrecision(18, 4);
            posSalesFlowBuilder.Property(x => x.discountMoney).HasPrecision(18, 4);
            posSalesFlowBuilder.Property(x => x.saleWay).IsRequired().HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.promoteSalesNo).HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.memberID).HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.cashRegisterID).HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.cashierID).HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.cashierName).HasMaxLength(50);
            posSalesFlowBuilder.Property(x => x.authorizationCashierID).HasMaxLength(100);
            posSalesFlowBuilder.Property(x => x.operateTime).HasColumnType("datetime");
            posSalesFlowBuilder.Ignore(x => x.Warehouse)
                .Ignore(x => x.GoodsInfo)
                .Ignore(x => x.GoodsCategory)
                .Ignore(x => x.PosCashier)
                .Ignore(x => x.PosCashRegister)
                .Ignore(x => x.PosMember)
                .Ignore(x => x.Supplier);

            //供应商
            var supplierBuilder = modelBuilder.Entity<Supplier>().ConfigBaseEntity().ConfigNo();
            supplierBuilder.ToTable("supplier");
            supplierBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            supplierBuilder.Property(x => x.supplierName).IsRequired().HasMaxLength(50);
            supplierBuilder.Property(x => x.linkman).HasMaxLength(50);
            supplierBuilder.Property(x => x.phone).HasMaxLength(50);
            supplierBuilder.Property(x => x.managingMode).IsRequired();

            //字典
            var sysDicBuilder = modelBuilder.Entity<SysDicsEntity>();
            sysDicBuilder.ToTable("Sys_Dics");

            //付款方式表
            var posPaymentTypeBuilder = modelBuilder.Entity<PosPaymentType>().ConfigBaseEntity();
            posPaymentTypeBuilder.ToTable("pos_payment_type");
            posPaymentTypeBuilder.Property(x => x.payWay).IsRequired().HasMaxLength(10);
            posPaymentTypeBuilder.Property(x => x.payWayName).IsRequired().HasMaxLength(50);
            posPaymentTypeBuilder.Property(x => x.status).IsRequired();

            //会员积分规则表
            var posMemberPromotionRuleBuilder = modelBuilder.Entity<PosMemberPromotionRule>().ConfigBaseEntity().ConfigReceiptEntity().ConfigWarehouse();
            posMemberPromotionRuleBuilder.ToTable("pos_member_promotion_rule");
            posMemberPromotionRuleBuilder.Property(x => x.promotionType).IsRequired().HasMaxLength(10);
            posMemberPromotionRuleBuilder.Property(x => x.integralRule).IsRequired().HasPrecision(18, 4);
            posMemberPromotionRuleBuilder.Property(x => x.discountRate).IsRequired().HasPrecision(18, 4);
            posMemberPromotionRuleBuilder.Property(x => x.remark).HasMaxLength(200);
            posMemberPromotionRuleBuilder.Ignore(x => x.Warehouse);

            //会员积分抵现/兑换规则表 add20241009
            var posMemberIntegralUsageRuleBuilder = modelBuilder.Entity<PosIntegralUsageRule>().ConfigBaseEntity().ConfigReceiptEntity().ConfigWarehouse();
            posMemberIntegralUsageRuleBuilder.ToTable("pos_integral_usage_rule");
            posMemberIntegralUsageRuleBuilder.Property(x => x.exchangeAmount).IsRequired().HasPrecision(18, 4);
            posMemberIntegralUsageRuleBuilder.Property(x => x.usageIntegral).IsRequired().HasPrecision(18, 4);
            posMemberIntegralUsageRuleBuilder.Property(x => x.amountThreshold).IsRequired().HasPrecision(18, 4).HasDefaultValue(0);
            posMemberIntegralUsageRuleBuilder.Property(x => x.upperLimitAmount).IsRequired().HasPrecision(18, 4).HasDefaultValue(0);
            posMemberIntegralUsageRuleBuilder.Property(x => x.exchangeUpperAmount).IsRequired().HasPrecision(18, 4).HasDefaultValue(0);
            posMemberIntegralUsageRuleBuilder.Property(x => x.remark).HasMaxLength(200);
            posMemberIntegralUsageRuleBuilder.Ignore(x => x.Warehouse);
        }
    }
}
